package net.sf.midollo.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Utils {

  private static IUtilsImpl instance;

  private static IUtilsImpl getInstance() {
    if (instance == null) {
      synchronized (Utils.class) {
        if (instance == null) {
          try {
            instance = (IUtilsImpl)Class.forName("net.sf.midollo.utils.UtilsImpl").newInstance();
          } catch (Exception ex) {
            throw new RuntimeException(ex.toString());
          }
        }
      }
    }
    return instance;
  }

  public static void log(String msg) {
    getInstance().log(msg);
  }

  public static List newList() {
    return getInstance().newList();
  }

  public static List newList(int initialCapacity) {
    return getInstance().newList(initialCapacity);
  }

  public static Map newMap() {
    return getInstance().newMap();
  }

  public static void writeTo(ByteArrayOutputStream baos, OutputStream os) throws IOException {
    getInstance().writeTo(baos, os);
  }

  public static String[] toStringArray(List list) {
    if (list == null)
      return null;
    String[] array = new String[list.size()];
    Object item;
    for (int i = 0; i < array.length; ++i) {
      item = list.get(i);
      array[i] = item == null ? null : item.toString();
    }
    return array;
  }

  public static int parseInt(String str, int defaultValue) {
    try {
      if (str == null)
        return defaultValue;
      return Integer.parseInt(str);
    } catch (NumberFormatException e) {
      return defaultValue;
    }
  }

  public static byte[] stringToBytes(String str) {
    int len = str.length();
    byte[] buffer = new byte[len];
    for (int idx = 0; idx < len; ++idx)
      buffer[idx] = (byte)str.charAt(idx);
    return buffer;
  }

  public static List split(String str, String delim) {
    List list = newList();
    String item;
    int idxLast = 0;
    int idx = 0;

    if (str != null) {
      while (true) {
        idx = str.indexOf(delim, idxLast);
        if (idx < 0) {
          item = str.substring(idxLast);
          list.add(item);
          break;
        }
        item = str.substring(idxLast, idx);
        list.add(item);
        idxLast = idx + delim.length();
      }
    }

    return list;
  }

  public static Object[] parseHostAndPort(String address) {
    String host = address;
    Integer port = null;
    if (address != null) {
      int idx = address.lastIndexOf(':');
      if (idx >= 0) {
        host = address.substring(0, idx);
        String strPort = address.substring(idx + 1);
        try {
          port = Integer.valueOf(strPort);
        } catch (NumberFormatException e) {
        }
      }
    }
    return new Object[] { host, port };
  }

  public static String getParsedHost(Object[] hostAndPort, String defaultHost) {
    String host = null;
    if (hostAndPort.length >= 1)
      host = (String)hostAndPort[0];
    if (host == null)
      return defaultHost;
    return host;
  }

  public static int getParsedPort(Object[] hostAndPort, int defaultPort) {
    Integer port = null;
    if (hostAndPort.length >= 2)
      port = (Integer)hostAndPort[1];
    if (port == null)
      return defaultPort;
    return port.intValue();
  }
}
